package fr.vvins.core.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.Path;
import javax.ws.rs.core.PathSegment;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fr.vvins.core.biz.catalog.CatalogException;
import fr.vvins.core.biz.community.CommunityException;
import fr.vvins.core.biz.community.CommunityManager;
import fr.vvins.core.biz.exception.BizException;
import fr.vvins.core.biz.user.dto.UserCommentDto;
import fr.vvins.core.biz.user.dto.UserLogDto;
import fr.vvins.core.biz.user.dto.UserRatingDtoInfo;
import fr.vvins.core.common.TechnicalException;

@Path("/community")
@Component("communityService")
public class CommunityServiceImpl implements CommunityService {
	@Autowired
	private CommunityManager communityManager;

	@Override
	public Set<UserLogDto> getLogs(String contentId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(contentId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "Content id must not be empty");
		}
		if (contentId.startsWith("content:")) {
			contentId = contentId.replace("content:", "");
			return getCommunityManager().getLogsByContent(Integer.parseInt(contentId));
		} else if (contentId.startsWith("key:")) {
			contentId = contentId.replace("key:", "");
			return getCommunityManager().getLogsByKey(contentId);
		}
		return getCommunityManager().getLogsByContent(Integer.parseInt(contentId));
	}

	@Override
	public Map<Integer,Set<UserLogDto>> getLogs(String contentId, List<PathSegment> actionId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(contentId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "Content id must not be empty");
		}
		if(actionId == null){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "Action id must not be empty");
		}
		List<Integer> actionIds = new ArrayList<Integer>();
		for(PathSegment id : actionId){
			actionIds.add(Integer.parseInt(id.getPath()));
		}
		if (contentId.startsWith("content:")) {
			contentId = contentId.replace("content:", "");
			return getCommunityManager().getLogsByContent(Integer.parseInt(contentId),actionIds);
		} else if (contentId.startsWith("key:")) {
			contentId = contentId.replace("key:", "");
			return getCommunityManager().getLogsByKey(contentId, actionIds);
		}
		return getCommunityManager().getLogsByContent(Integer.parseInt(contentId),actionIds);
	}

	@Override
	public Set<UserLogDto> getLogsByUser(String userId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(userId)){
			throw new CommunityException(CatalogException.EMPTY_PARAM, "User id must not be empty");
		}
		
		return getCommunityManager().getLogsByUser(Integer.parseInt(userId));
	}

	@Override
	public Set<UserCommentDto> getComments(String contentId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(contentId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "Content id must not be empty");
		}
		if (contentId.startsWith("content:")) {
			contentId = contentId.replace("content:", "");
			return getCommunityManager().getUserCommentByContent(Integer.parseInt(contentId));
		} else if (contentId.startsWith("key:")) {
			contentId = contentId.replace("key:", "");
			return getCommunityManager().getUserCommentByKey(contentId);
		}
		return getCommunityManager().getUserCommentByContent(Integer.parseInt(contentId));
	}

	@Override
	public Set<UserCommentDto> getCommentsByUser(String userId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(userId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "User id must not be empty");
		}
		
		return getCommunityManager().getUserCommentByContent(Integer.parseInt(userId));
	}

	@Override
	public Set<UserRatingDtoInfo> getRatings(String contentId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(contentId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "Content id must not be empty");
		}
		if (contentId.startsWith("content:")) {
			contentId = contentId.replace("content:", "");
			return getCommunityManager().getUserRatingByContent(Integer.parseInt(contentId));
		} else if (contentId.startsWith("key:")) {
			contentId = contentId.replace("key:", "");
			return getCommunityManager().getUserRatingByKey(contentId);
		}
		return getCommunityManager().getUserRatingByContent(Integer.parseInt(contentId));
	}

	@Override
	public Set<UserRatingDtoInfo> getRatingsByUser(String userId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(userId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "User id must not be empty");
		}
		
		return getCommunityManager().getUserRatingByContent(Integer.parseInt(userId));
	}

	public CommunityManager getCommunityManager() {
		return communityManager;
	}

	public void setCommunityManager(CommunityManager communityManager) {
		this.communityManager = communityManager;
	}
}
